package com.wondertek.liveroom;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class MyPagerAdapter extends PagerAdapter {
	
	private Context context = null;
	//private List<View> listData = null;
	private JSONArray jsonData = null;
	
	private static final int [] ImageResources = {
		R.drawable.bg_channel_1, R.drawable.bg_channel_2,
		R.drawable.bg_channel_3, R.drawable.bg_channel_4,
		R.drawable.bg_channel_5,
	};
	
	public MyPagerAdapter(Context context, JSONArray json) {
		this.context = context;
		//this.jsonData = json;
		createData();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		PageItemView view = (PageItemView) object;
		view.recycle();
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		PageItemView view = new PageItemView(context);
		try {
			view.setItemData((JSONObject) this.jsonData.get(position));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		((ViewPager)container).addView(view);
		
		return view;
	}

	@Override
	public int getCount() {
		//Log.e("AAAAAAAAA", "" + this.jsonData.length());
		return this.jsonData.length();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	
	private void createData() {
		this.jsonData = new JSONArray();
		for(int i=0; i<10; i++) {
			JSONObject obj = new JSONObject();
			
			try {
				obj.put("itemId", ImageResources[i%5]);
				obj.put("itemName", "Item  "+ i);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			jsonData.put(obj);
		}		
	}

}
